#寻找最优K值
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
import matplotlib.pyplot as plt
#导入并拆分数据集
dataset=load_iris()
x,y=dataset.data,dataset.target
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=0,test_size=0.5)
#预设K值范围
k_range=range(1,15)
k_error=[]
for k in k_range:
    model=KNeighborsClassifier(k)
    scores = cross_val_score(model,x,y,cv=5,scoring='accuracy')
    k_error.append(1-scores.mean())
#画图
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.plot(k_range,k_error,'r-')
plt.xlabel('k值')
plt.ylabel('准确率')
plt.show()
#开始训练模型
from sklearn.ensemble import BaggingClassifier
#定义K近邻模型
model_knn=KNeighborsClassifier(6)
model_knn.fit(x_train,y_train)
pred_knn=model_knn.predict(x_test)
ac_knn=accuracy_score(y_test,pred_knn)
print(f'K近邻模型的预测准确率：{ac_knn}')
#定义Bagging模型
model_bagging=BaggingClassifier(model_knn,n_estimators=130,max_samples=0.4,max_features=4,random_state=1)
model_bagging.fit(x_train,y_train)
pred_bagging=model_bagging.predict(x_test)
ac_bagging=accuracy_score(y_test,pred_bagging)
print(f'Bagging模型的预测准确率：{ac_bagging}')